package com.itheima.wechat;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference(cluster = "failfast")
    SetmealService setmealService;

    @RequestMapping("/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> setmeals = setmealService.getSetmeal();
        return Result.success("",setmeals);//数据一定要返回
    }

    public static void main(String[] args) {
        String url = "http://127.0.0.1:84/pages/setmeal_detail.html?id=5&name=jack&age=19";

//        System.out.println(getParams(url,"id"));
//        System.out.println(getParams(url,"name"));
        System.out.println(getParams(url,"age1"));
    }


    private static String getParams(String url,String key){
        String[] urlArray = url.split("\\?");//["http://127.0.0.1:84/pages/setmeal_detail.html","id=5&name=jack&age=19"]
        String params = urlArray[1];//id=5&name=jack&age=19
        String[] paramArray = params.split("&");//["id=5","name=jack","age=19"]

        for (String s : paramArray) {
            String[] p = s.split("=");//["id","5"]
            if(p[0].equals(key)){
                return p[1];
            }
        }

        return "";
    }

    @RequestMapping("/findById")
    public Result findById(Integer id,String token){
//        if(token == null){
//            return Result.error("401");
//        }
        //去redis获取用户信息根据token


        Setmeal setmeal = setmealService.findByIdBatch(id);
        return Result.success("",setmeal);
    }

    @RequestMapping("/findDetailById")
    public Result findDetailById(Integer id){
        Setmeal setmeal = setmealService.findDetailById(id);
        return Result.success("",setmeal);
    }
}
